#include "stdafx.h"
#include "Shader.h"

using namespace mgfx;

Shader::Shader()
{
	mInitialized = false;
	Effect = NULL;
	Tech = NULL;

	RasterizerState = NULL;
	DepthStencilState = NULL;
	StencilRef = 0;
	BlendState = NULL;
}

bool Shader::load(ID3D10Device * device, ShaderID id)
{
	ID = id;

	std::wstring path = L"content\\fx\\";
	path += mFilename;

	LOG("AA");

	DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
    shaderFlags |= D3D10_SHADER_DEBUG;
	shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
#else
	shaderFlags |= D3D10_SHADER_OPTIMIZATION_LEVEL3;
#endif
  
	ID3D10Blob* compilationErrors = 0;
	HRESULT hr = 0;
	hr = D3DX10CreateEffectFromFile(path.c_str(), 0, 0, 
		"fx_4_0", shaderFlags, 0, device, 0, 0, &Effect, &compilationErrors, 0);
	LOG("A");
	if(FAILED(hr))
	{
		LOG("B");
		if( compilationErrors )
		{
			LOG("C");
			MessageBoxA(0, (char*)compilationErrors->GetBufferPointer(), 0, 0);
			ReleaseCOM(compilationErrors);
		}
		DXTrace(__FILE__, (DWORD)__LINE__, hr, path.c_str(), true);

		return false;
	}

	LOG("D");

	return init();

}

void Shader::setStates(ID3D10Device* device)
{
	FLOAT blendFactors[4] = {0, 0, 0, 0};
	device->RSSetState(RasterizerState);
	device->OMSetDepthStencilState(DepthStencilState, StencilRef);
	device->OMSetBlendState(BlendState, blendFactors, 0xFF);
}